package Service.ServiceImpl;


import Dao.empDao;
import Model.Emp1;
import Service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tools.initLogRecord;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class EmpService implements UserService {

    private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;
    private static empDao empDao;

    public static void init() throws IOException {
        //1.读取加载核心配置文件
        in = Resources.getResourceAsStream("config/mybatis-config2.xml");
        //2.构造会话工厂
        factory = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        sqlSession = factory.openSession();
        empDao = sqlSession.getMapper(empDao.class);
    }

    //增加
    @Override
    public void addEmp(HashMap hashMap) throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();

        empDao.addEmp(hashMap);
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }

    //查询
    @Override
    public void showEmp() throws IOException {
        System.out.println("业务逻辑开始了");
        initLogRecord.initLog();
        init();
        List<Emp1> all = empDao.showEmpDao();
        for (Emp1 o : all) {
            System.out.println(o);
        }
    }


    //删除
    @Override
    public void delEmp(int empId) throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        empDao.deleteDao(empId);
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }


    //修改
    @Override
    public void updateEmp(int empId1) throws SQLException, IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        Emp1 one = empDao.findOne(empId1);
        Scanner scanner = new Scanner(System.in);
        if (one == null) {
            System.out.println("no");
        } else {
            Emp1 obj = new Emp1();
            System.out.println("你要更新的房屋姓名是:" + one.getName());
            System.out.print("请输入你要更新的房屋是:");
            String s = scanner.nextLine();
            if (s.equals("")) {
                obj.setName(one.getName());
            } else {
                obj.setName(s);
            }

            System.out.println("你要更新的电话是:" + one.getTel());
            System.out.print("请输入你要更新的电话是:");
            String s1 = scanner.nextLine();
            if (s1.equals("")) {
                obj.setTel(one.getTel());
            } else {
                obj.setTel(s1);
            }

            System.out.println("你要更新的地址是:" + one.getDress());
            System.out.print("请输入你要更新的地址是:");
            String s2 = scanner.nextLine();
            if (s2.equals("")) {
                obj.setDress(one.getDress());
            } else {
                obj.setDress(s2);
            }

            System.out.println("你要更新的钱是:" + one.getMoney());
            System.out.print("请输入你要更新的钱是:");
            String s3 = scanner.nextLine();
            if (s3.equals("")) {
                obj.setMoney(one.getMoney());
            } else {
                int a = Integer.parseInt(s3);
                obj.setMoney(a);
            }

            System.out.println("你要更新的状态是:" + one.getSta());
            System.out.print("请输入你要更新的状态是:");
            String s4 = scanner.nextLine();
            if (s4.equals("")) {
                obj.setSta(one.getSta());
            } else {
                obj.setSta(s4);
            }
            obj.setEmpId(empId1);
            empDao.updateEmpDao(obj);
            sqlSession.commit();
            sqlSession.close();
            in.close();
        }
    }
}
